package com.db4o.odbgen.plugins.java.maingenerator.typedxmlschema;

/**
 * 
 * Util methods for TypedXmlSchema.
 * @author liviug
 */
class Utils {
    public static class QualifiedName {
        private String _simpleName;
        private String _packageName;
        public QualifiedName(String packageName, String simpleName){
            this._packageName = packageName;
            this._simpleName = simpleName;
        }
        public String getSimpleName() {
            return _simpleName;
        }
        public String getPackageName() {
            return _packageName;
        }
        public String getQualifiedName() {
            return this._packageName+(this._packageName.length()==0?"":".")+this._simpleName;
        }
    }

    /**
     * Breaks a qualified name into int's simple and package names.
     * @param qualifiedName
     * @return
     */
    public static Utils.QualifiedName getPackageAndSimpleName(String qualifiedName){
        //Get simple name and package name.
        qualifiedName = qualifiedName.replaceAll("\\s", "");
        String[] components = qualifiedName.split("\\.");
        QualifiedName res;
        if(components.length==1){
            res = new QualifiedName("", components[0]);
        }
        else{
            String simpleName = components[components.length-1];
            String packageName =qualifiedName.substring(
                    0, 
                    qualifiedName.length()-simpleName.length()-1
                    );
            res = new QualifiedName(packageName, simpleName);
        }
        
        return res;
    }

    /**
     * Returns whether a given name is a qualified or a simple name.
     * @param typeName The name to verify.
     * @return Returns true in case of a qualified name.
     * Returns false in case of a simple name.
     */
    public static boolean isQualifiedName(String typeName) {
        QualifiedName qname = Utils.getPackageAndSimpleName(typeName);
        if(qname.getPackageName().length()==0)
            return false;
        else
            return true;
    }
    
    
}




